﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FA_TOOL_SOFTWARE
{
    public enum Logic_Byte_Bit:byte
    {
        Bit0=0x01,
        Bit1=0x02,
        Bit2=0x04,
        Bit3=0x08,
        Bit4=0x10,
        Bit5=0x20,
        Bit6=0x40,
        Bit7=0x80
    }
    partial class Form1
    {
        List<byte> g_ReceivedUartDataList = new List<byte>();

        private void Receiving_Data_UnPAcking_By_USBPacket(byte Usb_Cmd, byte[] Usb_Data)
        {


                //byte Usb_Cmd = ReceivedClass.USB_Cmd;
                //byte[] Usb_Data = (byte[])ReceivedClass.USB_ReceivedData.Clone();
                SetText("Receiving_Data_ByEvent CMD : " + HM_Utilitys.ByteToHexString(Usb_Cmd) + Environment.NewLine);
                SetText("Receive_Data byte: " + HM_Utilitys.ByteArrayToHexString(Usb_Data) + Environment.NewLine);

                //LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group group = LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.None;
                List<LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group> outDataGroupList;
                List<byte[]> outDataList;

                if (Usb_Cmd == (byte)USB_CDC_Cmd.Cmd_UART_RS485_Receive_Data)
                {
                    g_ReceivedUartDataList.AddRange(Usb_Data);
                    SetText("Cmd_UART_RS485_Receive_Data byte: " + HM_Utilitys.ByteArrayToHexString(Usb_Data) + Environment.NewLine);
                    SetText("Cmd_UART_RS485_Receive_Data List : " + HM_Utilitys.ByteArrayToHexString(g_ReceivedUartDataList.ToArray<byte>()) + Environment.NewLine);
                    LEV_One_Wire_Receiving_Packet_Decoding.Packet_Decoding_To_Group_And_Clear_List(ref g_ReceivedUartDataList, out outDataGroupList, out outDataList);

                    if ((outDataGroupList.Count >= 1) && (outDataGroupList.Count == outDataList.Count))
                    {
                        for (int i = 0; i < outDataGroupList.Count; i++)
                        {
                            if (outDataGroupList[i] == LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.OneWire_SystemData_Group)
                            {
                                SetText("OneWire_SystemData_Group : " + Environment.NewLine);
                                SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                            }
                            else if (outDataGroupList[i] == LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.OneWire_EEPROM_Group)
                            {
                                SetText("OneWire_EEPROM_Group : " + Environment.NewLine);
                                SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                            }
                            else
                            {
                                SetText("[Fail] None OneWire_Data : " + outDataGroupList[i] + Environment.NewLine);
                                SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                            }
                        }
                    }//if (outDataGroupList.Count >= 1)
                    else
                    {
                        SetText("[Fail] OneWire_Data List Count Fail.  " + Environment.NewLine);
                        LEV_One_Wire_Receiving_Packet_Decoding.Packet_Decoding_To_Group_And_Clear_List(ref g_ReceivedUartDataList, out outDataGroupList, out outDataList);
                    }
                }//if(Usb_Cmd == (byte)USB_CDC_Cmd.Cmd_UART_RS485_Receive_Data){

        }//private void Receiving_Data_UnPAcking_By_USBPacket(byte Usb_Cmd, byte[] Usb_Data)

        private void Set_OneWire_SystemData(byte[] data)
        {
            int DOC_Index = 4;
            Logic_Byte_Bit DOC_Set_Bit = Logic_Byte_Bit.Bit0;
            bool DOC = false;
            DOC = ((data[DOC_Index] & (byte)DOC_Set_Bit) > 0) ? true : false;

            int COC_Index = 4;
            Logic_Byte_Bit COC_Set_Bit = Logic_Byte_Bit.Bit1;
            bool COC = false;
            COC = ((data[COC_Index] & (byte)COC_Set_Bit) > 0) ? true : false;

            int BatOV_Index = 4;
            Logic_Byte_Bit BatOV_Set_Bit = Logic_Byte_Bit.Bit2;
            bool BatOV = false;
            BatOV = ((data[BatOV_Index] & (byte)BatOV_Set_Bit) > 0) ? true : false;

            int BatUV_Index = 4;
            Logic_Byte_Bit BatUV_Set_Bit = Logic_Byte_Bit.Bit3;
            bool BatUV = false;
            BatUV = ((data[BatUV_Index] & (byte)BatUV_Set_Bit) > 0) ? true : false;

            int CHG_OT_Index = 4;
            Logic_Byte_Bit CHG_OT_Set_Bit = Logic_Byte_Bit.Bit6;
            bool CHG_OT = false;
            CHG_OT = ((data[CHG_OT_Index] & (byte)CHG_OT_Set_Bit) > 0) ? true : false;

            int DSG_OT_Index = 4;
            Logic_Byte_Bit DSG_OT_Set_Bit = Logic_Byte_Bit.Bit7;
            bool DSG_OT = false;
            DSG_OT = ((data[DSG_OT_Index] & (byte)DSG_OT_Set_Bit) > 0) ? true : false;

            int CHG_MOS_Index = 11;
            Logic_Byte_Bit CHG_MOS_Set_Bit = Logic_Byte_Bit.Bit0;
            bool CHG_MOS = false;
            CHG_MOS = ((data[CHG_MOS_Index] & (byte)CHG_MOS_Set_Bit) > 0) ? true : false;

            int DSG_MOS_Index = 11;
            Logic_Byte_Bit DSG_MOS_Set_Bit = Logic_Byte_Bit.Bit1;
            bool DSG_MOS = false;
            DSG_MOS = ((data[DSG_MOS_Index] & (byte)DSG_MOS_Set_Bit) > 0) ? true : false;

            int DSG_Current_ADC_hi_index = 16;
            int DSG_Current_ADC_lo_index = 17;
            int DSG_Current_ADC = (data[DSG_Current_ADC_hi_index] << 8) + data[DSG_Current_ADC_lo_index];

            int CHG_Current_ADC_hi_index = 18;
            int CHG_Current_ADC_lo_index = 19;
            int CHG_Current_ADC = (data[CHG_Current_ADC_hi_index] << 8) + data[CHG_Current_ADC_lo_index];

            int VBAT_ADC_hi_index = 20;
            int VBAT_ADC_lo_index = 21;
            int VBAT_ADC = (data[VBAT_ADC_hi_index] << 8) + data[VBAT_ADC_lo_index];

            int NTC1_ADC_hi_index = 22;
            int NTC1_ADC_lo_index = 23;
            int NTC1_ADC = (data[NTC1_ADC_hi_index] << 8) + data[NTC1_ADC_lo_index];

            int Current_Capacity_hi_index = 28;
            int Current_Capacity_lo_index = 29;
            int Current_Capacity = (data[Current_Capacity_hi_index] << 8) + data[Current_Capacity_lo_index];

            int Cycle_Count_RECORD_hi_index = 56;
            int Cycle_Count_RECORD_lo_index = 57;
            int Cycle_Count_RECORD = (data[Cycle_Count_RECORD_hi_index] << 8) + data[Cycle_Count_RECORD_lo_index];


        }

        private void Set_OneWire_EEPROMData(byte[] data)
        {
            float tempf = 0.0f;

            //////////////////////////////////////////////////////////////////////////////
            int CHG_mA_To_ADC_Factor_float_hi_index = 0;
            tempf = 0.0f;
            unsafe
            {
                byte* byteTemp = (byte*)&tempf;
                *byteTemp++ = data[CHG_mA_To_ADC_Factor_float_hi_index + 3];
                *byteTemp++ = data[CHG_mA_To_ADC_Factor_float_hi_index + 2];
                *byteTemp++ = data[CHG_mA_To_ADC_Factor_float_hi_index + 1];
                *byteTemp++ = data[CHG_mA_To_ADC_Factor_float_hi_index];
            }
            float CHG_mA_To_ADC_Factor = tempf; 
   
            //////////////////////////////////////////////////////////////////////////////
            int DSG_mA_To_ADC_Factor_float_hi_index = 4;
            tempf = 0.0f;
            unsafe
            {
                byte* byteTemp = (byte*)&tempf;
                *byteTemp++ = data[DSG_mA_To_ADC_Factor_float_hi_index + 3];
                *byteTemp++ = data[DSG_mA_To_ADC_Factor_float_hi_index + 2];
                *byteTemp++ = data[DSG_mA_To_ADC_Factor_float_hi_index + 1];
                *byteTemp++ = data[DSG_mA_To_ADC_Factor_float_hi_index];
            }
            float DSG_mA_To_ADC_Factor = tempf; 
   
            //////////////////////////////////////////////////////////////////////////////
            int VBAT_mV_To_ADC_Factor_float_hi_index = 8;
            tempf = 0.0f;
            unsafe
            {
                byte* byteTemp = (byte*)&tempf;
                *byteTemp++ = data[VBAT_mV_To_ADC_Factor_float_hi_index + 3];
                *byteTemp++ = data[VBAT_mV_To_ADC_Factor_float_hi_index + 2];
                *byteTemp++ = data[VBAT_mV_To_ADC_Factor_float_hi_index + 1];
                *byteTemp++ = data[VBAT_mV_To_ADC_Factor_float_hi_index];
            }
            float VBAT_mV_To_ADC_Factor = tempf; 
   

            //////////////////////////////////////////////////////////////////////////////
            int DSG_OP_ADC_OFFSET_index = 16;
            sbyte DSG_OP_ADC_OFFSET = (sbyte)data[DSG_OP_ADC_OFFSET_index];
            //////////////////////////////////////////////////////////////////////////////
            int CHG_OP_ADC_OFFSET_index = 17;
            sbyte CHG_OP_ADC_OFFSET = (sbyte)data[CHG_OP_ADC_OFFSET_index];
            //////////////////////////////////////////////////////////////////////////////
            int VBAT_ADC_OFFSET_index = 18;
            sbyte VBAT_ADC_OFFSET = (sbyte)data[VBAT_ADC_OFFSET_index];
            //////////////////////////////////////////////////////////////////////////////
            int MANUFACTURE_NAME_LENGTH_index = 119;
            int MANUFACTURE_NAME_LENGTH = data[MANUFACTURE_NAME_LENGTH_index];
            int MANUFACTURE_NAME_index = 120;
            byte[] strByte = new byte[MANUFACTURE_NAME_LENGTH];
            Array.Copy(data, MANUFACTURE_NAME_index, strByte, 0, strByte.Length);
            string MANUFACTURE_NAME_String = System.Text.Encoding.Default.GetString(strByte);
            //////////////////////////////////////////////////////////////////////////////
            int MANUFACTURE_DATE_hi_index= 106;
            int MANUFACTURE_DATE_lo_index= 107;
            int MANUFACTURE_DATE_Value = (data[MANUFACTURE_DATE_hi_index] << 8) + data[MANUFACTURE_DATE_lo_index];
            DateTime MANUFACTURE_DATE = ManufactureData_Translate(MANUFACTURE_DATE_Value);
            //////////////////////////////////////////////////////////////////////////////

        }
        private void SetControlItems_Protection(bool COT, bool DOT, bool COC, bool DOC, bool OVP, bool UVP)
        {
            this.OT_Protect_CHG(COT);
            this.OT_Protect_DSG(DOT);
            this.OC_Protect_CHG(COC);
            this.OC_Protect_DSG(DOC);
            this.OV_Protect(OVP);
            this.UV_Protect(UVP);
        }
        private void SetControlItems_AdvanceInformation(bool COT, bool DOT, bool COC, bool DOC, bool OVP, bool UVP)
        {

        }
        private DateTime ManufactureData_Translate(int date)
        {
            //(MANUFACTURE_DATE_YEAR - 1980) * 512 + MANUFACTURE_DATE_MONTH * 32 + MANUFACTURE_DATE_DAY  // 2 bytes
            //MANUFACTURE_DATE_DAY = 5 bits, MANUFACTURE_DATE_MONTH = 4 bits, MANUFACTURE_DATE_YEAR = 7 bits
            int Day_Mask = 0x1f;
            int Month_Mask = 0x0f;

            int Day = (date & Day_Mask);
            int Month = ((date >> 5) & Month_Mask);
            int Year = (date >> 9);

            return new DateTime(Year, Month, Day);
        }

    }//partial class Form1
}
